﻿using System;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Reflection;
namespace Data
{
	 	//SF_Message
		public class SF_Message
	{	
		/// <summary>
        /// 主键
        /// </summary>
        private readonly string primaryKey= "Id";
		/// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public long Add(Model.SF_Message model)
        {
            return new DataAccess().Add<Model.SF_Message>(model, primaryKey);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Update(Model.SF_Message model)
        {
            return new DataAccess().Update<Model.SF_Message>(model, primaryKey);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public bool Delete(long Id)
        {
            return new DataAccess().Delete<Model.SF_Message>(Id, primaryKey);
        }
        /// <summary>
        /// 单条
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public Model.SF_Message GetModel(long Id)
        {
            return new DataAccess().Get<Model.SF_Message>(Id, primaryKey);
        }
        public Model.SF_Message GetModeldesc(long Id)
        {
            return new DataAccess().Get<Model.SF_Message>("UserID='" + Id + "' and i_type=1 order by Id desc ",null);
        }
        /// <summary>
        /// 总数据数
        /// </summary>
        /// <returns></returns>
        public int GetAll_Count()
        {
            return new DataAccess().GetCount<Model.SF_Message>(primaryKey);
        }
        /// <summary>
        /// 抽取分页数据
        /// </summary>
        public List<Model.SF_Message> GetPageList_Where(int PageIndex, int PageSize, long UserID, out int totalCount)
        {
            string strWhere = GetSqlWhere(UserID);
            totalCount = new DataAccess().GetCount<Model.SF_Message>(primaryKey, strWhere);
            return new DataAccess().GetPage_List<Model.SF_Message>(PageIndex, PageSize, primaryKey, primaryKey+" desc", strWhere);
        }

        public List<Model.SF_Message> GetPageList_Where(int PageIndex, int PageSize, long UserID,int type, out int totalCount)
        {
            string strWhere = GetSqlWhere(UserID, type);
            totalCount = new DataAccess().GetCount<Model.SF_Message>(primaryKey, strWhere);
            return new DataAccess().GetPage_List<Model.SF_Message>(PageIndex, PageSize, primaryKey, primaryKey + " desc", strWhere);
        }
        /// <summary>
        /// 拼接条件
        /// </summary>
        public string GetSqlWhere(long UserID)
        {
            StringBuilder sqlWhere = new StringBuilder();

            sqlWhere.Append(" 1>0");

            if (  UserID>0)
            {
                sqlWhere.Append(" and UserID =" + UserID );
            }         
            return sqlWhere.ToString();
        }
        public string GetSqlWhere(long UserID,int type)
        {
            StringBuilder sqlWhere = new StringBuilder();

            sqlWhere.Append(" 1>0");

            if (UserID > 0)
            {
                sqlWhere.Append(" and UserID =" + UserID);
            }
            if (type==0||type==1)
            {
                sqlWhere.Append(" and i_type =" + type);
            }
            return sqlWhere.ToString();
        }
        /// <summary>
        /// 批删
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        public int Drop(string Ids)
        {
            return new DataAccess().Drop<Model.SF_Message>(Ids, primaryKey);
        }
	}
}